package com.bigfreakindeals.Controller;


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Arrays;

import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.CoreProtocolPNames;
import org.apache.http.protocol.HTTP;


import oauth.signpost.OAuth;
import oauth.signpost.OAuthConsumer;
import oauth.signpost.OAuthProvider;
import oauth.signpost.basic.DefaultOAuthConsumer;
import oauth.signpost.basic.DefaultOAuthProvider;
import oauth.signpost.commonshttp.CommonsHttpOAuthConsumer;
import oauth.signpost.commonshttp.CommonsHttpOAuthProvider;
import oauth.signpost.exception.OAuthCommunicationException;
import oauth.signpost.exception.OAuthExpectationFailedException;
import oauth.signpost.exception.OAuthMessageSignerException;
import oauth.signpost.exception.OAuthNotAuthorizedException;
import twitter4j.Twitter;
import twitter4j.TwitterException;
import twitter4j.TwitterFactory;
import twitter4j.auth.AccessToken;
import twitter4j.auth.RequestToken;


import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.util.Log;


import com.bigfreakindeals.Common.Constant;

public class TwitterHelper {
	
	private SharedPreferences preferences;
	
	CommonsHttpOAuthConsumer consumer;
	CommonsHttpOAuthProvider provider;
	RequestToken requestToken = null;
   HttpClient client = new DefaultHttpClient();
   
   Twitter twitterConnection;
	public TwitterHelper() {
		
		consumer = new CommonsHttpOAuthConsumer(Constant.TWITTER_Consumerkey,Constant.TWITTER_ConsumerSecrer);

	   provider = new CommonsHttpOAuthProvider(
	            "http://twitter.com/oauth/request_token",
	            "http://twitter.com/oauth/access_token",
	            "http://twitter.com/oauth/authorize");
	   
	   
	}
	//public String accessTwitter() throws OAuthMessageSignerException, OAuthNotAuthorizedException, OAuthExpectationFailedException, OAuthCommunicationException, IOException{
		public String accessTwitter() throws TwitterException {
			
			twitterConnection = new TwitterFactory().getInstance();
			twitterConnection.setOAuthConsumer(Constant.TWITTER_Consumerkey,Constant.TWITTER_ConsumerSecrer);
			requestToken = twitterConnection.getOAuthRequestToken("");
		
			System.out.println("Token secret: " + requestToken.getTokenSecret());
			System.out.println("Token : " + requestToken.getToken());
			System.out.println("auth url : " + requestToken.getAuthorizationURL());
			   
			return (requestToken.getAuthorizationURL());
	
	    
	}
	public void sendTweet(String pin,String msg) throws TwitterException {
		System.out.println("PIN: " + pin);
		System.out.println("Request Token: " + requestToken.getToken());
	    System.out.println("Request Token secret: " + requestToken.getTokenSecret());
	   
		AccessToken accessToken = twitterConnection.getOAuthAccessToken(requestToken,pin);
		
		System.out.println("Access Ttoken: " + accessToken.getToken());
	    System.out.println("Access Token secret: " + accessToken.getTokenSecret());
	   
		twitterConnection.setOAuthAccessToken(accessToken);
		
	    twitterConnection.updateStatus(msg);

	}
	
	
	public void storeAccessToken() {
	/*	Editor editor = preferences.edit();
		editor.putString(TWEET_AUTH_KEY, consumer.getToken());
		editor.putString(TWEET_AUTH_SEKRET_KEY, consumer.getTokenSecret());
		editor.commit();
	*/
		}
}
